---
layout : doc
title : Filters in Phoenix
permalink: docs\Filters.html
---

## Phoenix Filters

Filters give you the ability to write code which can execute against any executing controller action. There are three types of filters, and you can register filters in a number of ways. Filters work very similar to asp.net and the behaviour/api's match ASP.net MVC.

### Action Filters
These filters are used to modify the behaviour of your action, this could be authentication, authorisation or checking network connections.

An example of an action filter which deals with network concern would look like this:

    public class RequiresNetworkAttribute : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                filterContext.Result = new MessageBoxActionResult(filterContext.ActionContext,
                    "No network connection, please connect and try the operation again");
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if(filterContext.Exception is NetworkException)
            {
                filterContext.Result = new MessageBoxActionResult(filterContext.ActionContext,
                    "A network error occured, please check your connection and try again");
                context.ExceptionHandled = true;
            }
        }
    }

There are two requirements to handle an error, you must set the ExceptionHandled to true, this will stop other filters being executed when an exception occurs, AND a Result must be set. If you set ExceptionHandled = true, but don't set a result, Phoenix will continue to execute filters.

### Exception filters
As you can see, Action Filters can also handle exceptions, or you can write dedicated exception filters. This may look like this:

    public class TrelloErrorFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            var trelloException = context.Exception as TrelloRequestFailedException;
            if (trelloException != null)
            {
                context.Result new MessageBoxActionResult(filterContext.ActionContext,trelloException.Message, "Trello Returned an Error", MessageBoxButton.OK);
                context.ExceptionHandled = true;
            }
        }
    }

This would be useful for known exception types, like known WCF exceptions, and other exceptions that you can cross cut and give a consistent behaviour when errors occur.

### Result Filters

Result filters allow you to modify the behaviour of the action results, if you wanted to add Caliburn Micro style view bindings, you could implement a result filter which provided those bindings.

## Implementing Filters

Phoenix has a static `FilterProviders` class, which you can register custom filter providers with Phoenix. By default the `FilterAttributeFilterProvider` is registered, which will find filters which inherit from `FilterAttribute` on the controller or action.

There are 3 main interfaces you need to be aware of, `IActionFilter`, `IResultFilter` and `IExceptionFilter`. To create a attribute based filter, simply create a class which looks like this:

    public class MyFilterAttribute : FilterAttribute, IActionFilter
    {
        //Implementation
    }
    
If you would like global filters, simply create your own filter provider by inheriting from `IFilterProvider`, which can return all your filters. Add your filter provider to the FilterProviders collection.